﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Drawing2D;

public partial class cn_imageedit_ImgSrc : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.Form["getsrc"]!=null)
            {
                if (Session["ImgPath"]!=null)
                {

                    string path = Server.MapPath("/uploadfiles/image/" + Session["ImgPath"].ToString().Replace("Big", "Small.jpg"));
                   // string path1 = Server.MapPath("../../uploadfiles/image/" + Session["ImgPath"].ToString() + ".jpg");
                   // Cutimage(path);
                    GetThumbImg(path,Convert.ToDouble( Request.Form["getsrc"].ToString()));
                    

                }
            }
        }
    }
    public void GetThumbImg(string src,double thumbParm)
    {
        String dest = src + "_th.jpg";
        System.Drawing.Image image = System.Drawing.Image.FromFile(src);
        int srcWidth = image.Width;
        int srcHeight = image.Height;
        double thumbHeight = srcHeight*thumbParm;
        double thumbWidth = srcWidth * thumbParm;
        int thHeight =Convert.ToInt32( thumbHeight);
        int thWidth = Convert.ToInt32(thumbWidth);
        Bitmap bmp = new Bitmap(thWidth, thHeight);
        System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
        //设置 System.Drawing.Graphics对象的SmoothingMode属性为HighQuality
        gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        //下面这个也设成高质量
        gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        //下面这个设成High
        gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
        //把原始图像绘制成上面所设置宽高的缩小图
        System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, thWidth, thHeight);
        gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel);
 
        bmp.Save(dest);
        bmp.Dispose();
        image.Dispose();
    }
    private void Cutimage(string imgpath)
    {

        System.Drawing.Image initImage = System.Drawing.Image.FromFile(imgpath);
        //原始图片的宽、高
        int initWidth = initImage.Width;
        int initHeight = initImage.Height;

        //非正方型先裁剪为正方型
        if (initWidth != initHeight)
        {
            //截图对象
            System.Drawing.Image pickedImage = null;
            System.Drawing.Graphics pickedG = null;

            //宽大于高的横图
            if (initWidth>initHeight)
            {
                //对象实例化
                pickedImage = new System.Drawing.Bitmap(145,145 );
                pickedG = System.Drawing.Graphics.FromImage(pickedImage);
                //设置质量
                pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                //定位
                Rectangle toR = new Rectangle(0, 0,145,145);


                Rectangle fromR= new Rectangle(initWidth/2 - 73, initHeight/2-73, 145, 145);

                //画图
               
                pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
                //重置宽
                initWidth = initHeight;
            }
            //高大于宽的竖图
            //else
            //{
            //    //对象实例化
            //    pickedImage = new System.Drawing.Bitmap(initWidth, initWidth);
            //    pickedG = System.Drawing.Graphics.FromImage(pickedImage);
            //    //设置质量
            //    pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            //    pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            //    //定位
            //    Rectangle fromR = new Rectangle(0, (initHeight - initWidth) / 2, initWidth, initWidth);
            //    Rectangle toR = new Rectangle(0, 0, initWidth, initWidth);
            //    //画图
            //    pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
            //    //重置高
            //    initHeight = initWidth;
            //}

            //将截图对象赋给原图
            System.Drawing.Image newimage = (System.Drawing.Image)pickedImage.Clone();
            //释放截图资源

            Bitmap bp = new Bitmap(newimage);
            bp.Save(imgpath + "_cut.jpg");
            bp.Dispose();
            pickedG.Dispose();
            pickedImage.Dispose();
        }
    }
}